<?php
/**
 * BEARスクリプトライブラリ
 *
 * @package BEAR
 * @subpackage script
 */


define('APP_DESCRIPTION', 'BEAR web application.');
define('DEVELOPER_NAME_SHORT', 'anonymous');
define('DEVELOPER_NAME_LONG', 'anonymous');
define('DEVELOPER_NAME_EMAIL', 'anonymous@example.com');
define('BEAR_PATH', '/user/share/php');
/**
 * package.xml生成
 *
 * BEARのパッケージ生成のためのpackage.xmlファイルを生成します。
 * <pre>
 * -------------------------------------
 * Example. パッケージングスクリプト実行準備
 *
 * PEAR_PackageFileManagerパッケージインストール
 *
 * </pre><code>
 * $ sudo pear config-set preferred-state beta
 * $ sudo pear install --alldeps PEAR/PEAR_PackageFileManager
 * </code>
 * <pre>
 * パッケージ生成
 *
 * </pre><code>
 * $ php make_package.php
 * $ php make_package.php __uri make
 * $ cd ..
 * $ pear package-validate package.xml
 * $ pear package
 * </code>
 * <pre>
 * パッケージインストール
 *
 * </pre><code>
 * $ pear install <Application>-x.x.x.tar.gz
 *
 * または
 *
 * $pear channel-discovier <Channel URI>
 * $pear install <Channel Alias>/<Application>
 *
 * </code>
 *
 * @package     App
 * @subpackage  script
 * @author      $Author: koriyama $
 * @version     $Id: make_package.php 184 2008-09-03 11:52:07Z koriyama $
 *
 */

// http://pear.php.net/package/PEAR_PackageFileManager/docs/1.6.0a4/PEAR_PackageFileManager/PEAR_PackageFileManager2.html#methodaddReplacement
/**
 * PackageFileManager2読み込み
 */
set_include_path(get_include_path() . PATH_SEPARATOR . BEAR_PATH);
require_once('PEAR/PackageFileManager2.php');
require_once('PEAR/PackageFileManager/File.php');   // avoid bugs
/**
 * App読み込み
 */
//require_once('BEAR/BEAR.php');
require_once('../../App.php');

PEAR::setErrorHandling(PEAR_ERROR_DIE);
$description = APP_DESCRIPTION;
$package = 'App';
$release_status = "beta";
$notes = App::NAME;
$version = App::VERSION;

$config = array(
'filelistgenerator' => 'file',
'packagedirectory' => dirname(dirname(dirname(__FILE__))).'/',
'baseinstalldir' => App::ID,
'ignore' => array('CVS/', 'package.xml', '.svn/', '.htaccess', 'beardev/', '.DS_Store', 'error.log', 'page.log'),
'changelogoldtonew' => false,
'description' => $description,
'dir_roles' => array('data'=>'data'),
'roles' => array('php'=>'data', 'tpl'=>'data')
);
$packagexml = new PEAR_PackageFileManager2();
$packagexml->setOptions($config);
$packagexml->setPackage($package);
$packagexml->setSummary($notes);
$packagexml->setDescription($description);

$packagexml->setAPIVersion($version);
$packagexml->setReleaseVersion($version);
$packagexml->setReleaseStability($release_status);
$packagexml->setAPIStability($release_status);
$packagexml->setNotes($notes);
$packagexml->setLicense('The BSD License', 'http://www.opensource.org/licenses/bsd-license.php');
$packagexml->setPackageType('php');
$packagexml->addRole('*', 'data');
$packagexml->setPhpDep('5.2.0');
$packagexml->setPearinstallerDep('1.4.0');
// Maintainer
$packagexml->addMaintainer('lead', DEVELOPER_NAME_SHORT , DEVELOPER_NAME_LONG, DEVELOPER_NAME_EMAIL);

$packagexml->addRelease();
// dependency
// dependency
$packagexml->addPackageDepWithChannel('required', 'BEAR', 'pear.bear-project.net', '0.1.1');
//$packagexml->addPackageDepWithChannel('required', 'Spreadsheet_Excel_Writer', 'pear.php.net', '0.9.0');

/**
 * user if no arg
 */
if (!isset($_SERVER['argv'][1])) {
	print ("Usage: php{$_SERVER['argv'][0]} package_uri uri|channel [make]\n");
	print ("ex)\n1) php {$_SERVER['argv'][0]} pear.example.co.jp channel \n");
	print ("2) php {$_SERVER['argv'][0]} pear.example.co.jp channel make\n");
	print ("3) cd ../../\n");
	print ("4) pear package-validate\n");
	print ("5) pear package\n");
	exit();
} else {
	$uri = $_SERVER['argv'][1];
}

/**
 * network install package or local package
 */
if ($_SERVER['argv'][2] == 'uri') {
	$packagexml->setUri($uri);
} else {
	$packagexml->setChannel($uri);
}
/**
 * this makes local install package
 */

$packagexml->generateContents();
// note use of debugPackageFile() - this is VERY important
if ($_SERVER['argv'][3] != 'debug') {
	debug_print("writePackageFile\n");
	$result = $packagexml->writePackageFile();
} else {
	$result = $packagexml->debugPackageFile();
	debug_print("debugPackageFile\n");
}

if (PEAR::isError($result)) {
	debug_print($result->getMessage()."\n");
	exit();
}
debug_print("End Script\n");

/**
 * デバック表示
 *
 * @param string $message
 */
function debug_print($message)
{
	return print($message);
}
